package com.ship.contract.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ship.common.business.service.impl.BaseServiceImpl;
import com.ship.common.core.base.SearchConfig;
import com.ship.common.core.exception.BusException;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.contract.mapper.SearchConfigMapper;
import com.ship.contract.service.SearchConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tangshixuan
 * @date 2023-8-31
 */
@Service
public class SearchConfigServiceImpl extends BaseServiceImpl<SearchConfigMapper, SearchConfig> implements SearchConfigService {
    @Autowired
    private SearchConfigMapper searchConfigMapper;

    @Override
    public List<SearchConfig> listByModuleAndUser(String module, String currentUserId) {
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("module",module);
        queryWrapper.eq("user_id",currentUserId);
        return searchConfigMapper.selectList(queryWrapper);
    }

    @Override
    public void saveConfig(SearchConfig searchConfig) {
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("module",searchConfig.getModule());
        queryWrapper.eq("user_id",searchConfig.getUserId());
        queryWrapper.eq("column_key",searchConfig.getColumnKey());
        SearchConfig existConfig = super.getOne(queryWrapper);
        if(existConfig != null){
            throw new BusException("该筛选项已存在!", BaseResultCode.GENERAL_ERROR);
        }
        super.save(searchConfig);
    }
}
